﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BBPA.BLL
{
    public partial class Message
    {
        /// <summary>
        /// 发送新消息
        /// </summary>
        /// <param name="ReplayID_"></param>
        /// <param name="UserID_"></param>
        /// <param name="ReceiveUserID_"></param>
        /// <param name="TheContent_"></param>
        /// <returns></returns>
        public CallBak<int> NewMessage(int ReplayID_, int UserID_, int ReceiveUserID_, string ReceiveUserIDStr_, int ReceiveUserTypeID_, string TheContent_, bool IsSendSMS_, bool IsSendWeb_)
        {

            CallBak<int> callBak = new CallBak<int>();
            Model.Message modelMessage = new Model.Message();
            modelMessage.ReplayID = ReplayID_;
            modelMessage.UserID = UserID_;
            modelMessage.ReceiveUserID = ReceiveUserID_;
            modelMessage.ReceiveUserIDStr = ReceiveUserIDStr_;
            modelMessage.ReceiveUserTypeID = ReceiveUserTypeID_;
            modelMessage.TheContent = TheContent_;
            //modelMessage.IsSendSMS = IsSendSMS_;
            //modelMessage.IsSendWeb = IsSendWeb_;
            //if (IsSendSMS_)
            //{
            //    BLL.SMS bllSMS = new SMS();
            //    bllSMS.NewSMS(ReceiveUserID_, ReceiveUserIDStr_,ReceiveUserTypeID_, TheContent_);
            //}
            callBak.ReturnVal = Add(modelMessage);
            return callBak;
        }
        public CallBak<int> NewMessage(int ReplayID_, int UserID_, int ReceiveUserID_, string TheContent_, bool IsSendSMS_, bool IsSendWeb_)
        {
            CallBak<int> callBak = NewMessage(ReplayID_, UserID_, ReceiveUserID_, "", 0, TheContent_, IsSendSMS_, IsSendWeb_);
            BLL.PAUser bllPAUser = new PAUser();
            AddUnReadMessageID2User(callBak.ReturnVal, bllPAUser.GetModel(ReceiveUserID_), IsSendSMS_);
            return callBak;
        }
        public CallBak<int> NewMessage(int ReplayID_, int UserID_, string ReceiveUserIDStr_, string TheContent_, bool IsSendSMS_, bool IsSendWeb_)
        {
            CallBak<int> callBak = NewMessage(ReplayID_, UserID_, 0, ReceiveUserIDStr_, 0, TheContent_, IsSendSMS_, IsSendWeb_);
            BLL.PAUser bllPAUser = new PAUser();
            foreach (Model.PAUser user in bllPAUser.GetModelList("id in (" + ReceiveUserIDStr_ + ")"))
                AddUnReadMessageID2User(callBak.ReturnVal, user, IsSendSMS_);
            return callBak;
        }
        public CallBak<int> NewMessage2UserType(int ReplayID_, int UserID_, int ReceiveUserTypeID_, string TheContent_, bool IsSendSMS_, bool IsSendWeb_)
        {
            CallBak<int> callBak = NewMessage(ReplayID_, UserID_, 0, "", ReceiveUserTypeID_, TheContent_, IsSendSMS_, IsSendWeb_);
            BLL.PAUser bllPAUser = new PAUser();
            foreach (Model.PAUser user in bllPAUser.GetModelList("UserType=" + ReceiveUserTypeID_))
                AddUnReadMessageID2User(callBak.ReturnVal, user, IsSendSMS_);
            return callBak;
        }
        /// <summary>
        /// 打印接收人
        /// </summary>
        /// <param name="message_"></param>
        /// <returns></returns>
        public static string PrintMessageReceiveUser(Model.V_Message message_)
        {
            return PrintMessageReceiveUser(message_.ReceiveUsername, message_.ReceiveUserIDStr, message_.ReceiveUserTypeID);
        }
        public static string PrintMessageReceiveUser(object ReceiveUsername_, object ReceiveUserIDStr_, object ReceiveUserTypeID_)
        {
            string _ReceiveUsername = DataTypePlus.requestString(ReceiveUsername_);
            string _ReceiveUserIDStr = DataTypePlus.requestString(ReceiveUserIDStr_);
            int _ReceiveUserTypeID = DataTypePlus.requestInt(ReceiveUserTypeID_);
            if (_ReceiveUsername != "") return _ReceiveUsername;
            if (_ReceiveUserIDStr != "")
            {
                string userNameStr = "";
                BLL.PAUser bllPAUser = new PAUser();
                foreach (Model.PAUser user in bllPAUser.GetModelList("id in (" + _ReceiveUserIDStr + ")"))
                    userNameStr += (userNameStr == "" ? "" : ",") + user.Username;
                return userNameStr;
            }
            if (_ReceiveUserTypeID > 0) return EnumHelper.GetEnumDescription((EnumHelper.UserType)_ReceiveUserTypeID);
            else return "全部";
        }
        /// <summary>
        /// 判断这个通知是否已读
        /// </summary>
        /// <param name="MessageID_"></param>
        /// <param name="ModelPAUser_"></param>
        /// <returns></returns>
        public static bool HasRead(object MessageID_, Model.PAUser ModelPAUser_)
        {
            return ("," + ModelPAUser_.UnReadMessageIDStr + ",").IndexOf("," + MessageID_ + ",") == -1;
        }
        /// <summary>
        /// 设置为已读
        /// </summary>
        /// <param name="MessageID_"></param>
        /// <param name="ModelPAUser_"></param>
        public static bool SetHasRead(int MessageID_, Model.PAUser ModelPAUser_)
        {
            string UnReadMessageIDStr = ("," + ModelPAUser_.UnReadMessageIDStr + ",").Replace("," + MessageID_.ToString() + ",", ",");
            UnReadMessageIDStr = UnReadMessageIDStr.Substring(1);
            if (UnReadMessageIDStr != "") UnReadMessageIDStr = UnReadMessageIDStr.Substring(0, UnReadMessageIDStr.Length - 1);
            if (UnReadMessageIDStr != ModelPAUser_.UnReadMessageIDStr)
            {
                if (UnReadMessageIDStr == ",") UnReadMessageIDStr = "";
                ModelPAUser_.UnReadMessageIDStr = UnReadMessageIDStr;
                BLL.PAUser bllPAUser = new PAUser();
                bllPAUser.Update(ModelPAUser_);
                return true;
            }
            else return false;
        }
        /// <summary>
        /// 给某个用户添加未读通知
        /// </summary>
        /// <param name="MessageID_"></param>
        /// <param name="ModelPAUser_"></param>
        public void AddUnReadMessageID2User(int MessageID_, Model.PAUser ModelPAUser_, bool IsSendSMS_)
        {
            string UnReadMessageIDStr = ModelPAUser_.UnReadMessageIDStr + (ModelPAUser_.UnReadMessageIDStr == "" ? "" : ",") + MessageID_.ToString();
            ModelPAUser_.UnReadMessageIDStr = UnReadMessageIDStr;
            BLL.PAUser bllPAUser = new PAUser();
            bllPAUser.Update(ModelPAUser_);

            if (IsSendSMS_)
            {
                BLL.SMS bllSMS = new SMS();
                bllSMS.NewSMS(ModelPAUser_.id, GetModel(MessageID_).TheContent);
            }
        }
    }
}
